#ifndef _DICOM_DB_SQLITE_WRAPPER_H_
#define _DICOM_DB_SQLITE_WRAPPER_H_

#include <sqlite3.h>
#include <string>
#include <gwbase/base/util.h>

namespace dicom {
namespace db {

class Stmt;
typedef boost::shared_ptr<Stmt> StmtPtr;

class Database;
typedef boost::shared_ptr<Database> DbPtr;

class Stmt {
 public:
  Stmt(Database* owner, const std::string& sql, sqlite3_stmt* stmt);
  ~Stmt();

  void BindInt32(int32_t v);
  void BindInt64(int64_t v);

  void BindText(const char* s);
  void BindText(const std::string& s);

  int32_t GetInt32(int pos);
  int64_t GetInt64(int pos);
  const char* GetText(int pos);

  void Step();
  bool eof() const { return eof_; }

 private:
  Database* owner_;
  const std::string sql_;
  ::sqlite3_stmt* stmt_;
  int bind_pos_;
  bool eof_;
};

class Database {
 public:
  static Status Open(const std::string& path, DbPtr* db);
  ~Database();

  StmtPtr Prepare(const std::string& sql);
  void RunSql(const std::string& sql);

  bool TableExsits(const std::string& table_name);

  const char* err_msg();

  void StmtDone() { outgong_stmt_ = false; }

 private:
  Database(::sqlite3* db) : db_(db), outgong_stmt_(false) {}
  ::sqlite3* db_;
  bool outgong_stmt_;
};
}
};

#endif  // _DICOM_DB_SQLITE_WRAPPER_H_
